package project.youkumenu.customview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;

import java.util.HashSet;
import java.util.Random;
import java.util.Set;

import project.youkumenu.R;
import project.youkumenu.utils.LogUtil;

/**
 * Created by lgs on 2018/3/8.
 */

public class CustomTitleView extends View {
    /**
     * 文本
     */
    private String mTitleText;

    /**
     * 文本的颜色
     */
    private int mTitleTextColor;

    /**
     * 文本的大小
     */
    private int mTitleTextSize;

    /**
     * 绘制时控制文本绘制的范围
     */
    private Rect mBound;
    private Paint mPaint;

    public void setTitleText(String mTitleText) {
        this.mTitleText = mTitleText;
    }

    public String getTitleText() {
        return mTitleText;
    }

    public CustomTitleView(Context context) {
        this(context, null);
    }

    public CustomTitleView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CustomTitleView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    /**
     * 获得我们所定义的自定义样式属性
     */
    private void init(Context context, @Nullable AttributeSet attrs) {
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.CustomTitleView);
        int n = ta.getIndexCount();
        for (int i = 0; i < n; i++) {
            int attr = ta.getIndex(i);
            switch (attr) {
                case R.styleable.CustomTitleView_titleText:
                    mTitleText = ta.getString(attr);
                    break;
                case R.styleable.CustomTitleView_titleTextColor:
                    // 默认颜色设置为黑色
                    mTitleTextColor = ta.getColor(attr, Color.BLACK);
                    break;
                case R.styleable.CustomTitleView_titleTextSize:
                    mTitleTextSize = ta.getDimensionPixelSize(attr, (int) TypedValue.applyDimension(
                            TypedValue.COMPLEX_UNIT_SP, 16, getResources().getDisplayMetrics()));
                    break;
            }
        }
        ta.recycle();

        /**
         * 获得绘制文本的宽和高
         */
        mPaint = new Paint();

        mPaint.setTextSize(mTitleTextSize);
        mBound = new Rect();
        //返回在边界(分配)的最小矩形包含所有的字符(text文本)
        mPaint.getTextBounds(mTitleText, 0, mTitleText.length(), mBound);

//        setOnClickListener(new OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                if (null != mClick) {
//                    mClick.onClick(view);
//                }
////                mTitleText = randomText();
////                postInvalidate();
//            }
//        });
    }

    public String randomText() {
        Random rand = new Random();
        Set<Integer> set = new HashSet<>();
        while (set.size() < 4) {
            set.add(rand.nextInt(10));
        }
        StringBuffer sb = new StringBuffer();
        for (Integer i : set) {
            sb.append("").append(i);
        }
        return sb.toString();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        //        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        measureText(widthMeasureSpec, heightMeasureSpec);
    }

    private void measureText(int widthMeasureSpec, int heightMeasureSpec) {
        int width = 0;
        int height = 0;

        /*
        设置宽度
         */
        int specMode = MeasureSpec.getMode(widthMeasureSpec);
        int specSize = MeasureSpec.getSize(widthMeasureSpec);

        if (specMode == MeasureSpec.EXACTLY) {//明确指定了
            width = getPaddingLeft() + getPaddingRight() + specSize;
        } else if (specMode == MeasureSpec.AT_MOST) {//一般为WARP_CONTENT
            width = getPaddingLeft() + getPaddingRight() + mBound.width();
        }

        /*
        设置高度
         */
        specMode = MeasureSpec.getMode(heightMeasureSpec);
        specSize = MeasureSpec.getSize(heightMeasureSpec);
        if (specMode == MeasureSpec.EXACTLY) {// 明确指定了
            height = getPaddingTop() + getPaddingBottom() + specSize;
        } else if (specMode == MeasureSpec.AT_MOST) {//一般为WARP_CONTENT
            height = getPaddingTop() + getPaddingBottom() + mBound.height();
        }
        //决定了当前View的大小
        setMeasuredDimension(width, height);
        LogUtil.i("width = " + width + ", height = " + height);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        drawText(canvas);
    }

    //绘制
    private void drawText(Canvas canvas) {
        mPaint.setColor(Color.BLACK);
        LogUtil.i("getMeasuredWidth = " + getMeasuredWidth() + ", getMeasuredHeight = " + getMeasuredHeight());

        canvas.drawRect(0, 0, getMeasuredWidth(), getMeasuredHeight(), mPaint);
        mPaint.setColor(mTitleTextColor);
        canvas.drawText(mTitleText, getWidth() / 2 - mBound.width() / 2,
                getHeight() / 2 + mBound.height() / 2, mPaint);

        //        invalidate();
    }

    public void onClickListener(OnViewClick click) {
        this.mClick = click;
    }

    private OnViewClick mClick;

    public interface OnViewClick {
        void onClick(View v);
    }
}
